package com.bruce.controller;

import com.bruce.bean.Major;
import com.bruce.service.MajorService;
import com.bruce.service.impl.MajorServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/majorServlet.do")
public class MajorServlet extends HttpServlet {

    MajorService majorService=new MajorServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String method = req.getParameter("flag");
        if("getMajors".equals(method)){
            //Ajax查询专业
            String collegeId = req.getParameter("collegeId");
            //根据院系ID查询下面所有专业
            List<Major> majors = majorService.findMajorsByCollegeId(Integer.valueOf(collegeId));
            Gson gson=new Gson();
            String json = gson.toJson(majors);
            resp.getWriter().write(json);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
